<?php
/**
 * Bootstrap_Abstract
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-02-24 00:58:45
 * 修改记录: 
 * 
 * $Id: Abstract.php 7 2011-03-16 05:07:39Z coosuu@gmail.com $
 */

class Bootstrap_Abstract {

    private $_options = array();

    public function __construct($options = array()) {

        $this->setOptions($options);
        $this->_registFunctions();
        $this->_registAutoload();
        $this->_registExceptionHandler();
    }

    public function setOptions($key, $value = null) {

        if (!$key) {
            return false;
        }

        if (is_array($key)) {
            $this->_options = array_merge($this->_options, $key);
        } else {
            $this->_options[$key] = $value;
        }

        return true;
    }

    public function getOption($key = null) {

        if (!$key) {
            return $this->_options;
        }

        if (!array_key_exists($key, $this->_options)) {
            return null;
        }

        return $this->_options[$key];
    }

    protected function _registAutoload() {

        if (function_exists('__autoload')) {
            return false;
        }

        // 系统函数
        spl_autoload_register(array('Bootstrap_Abstract', '_autoload'));
    }

    public function run() {

        throw new Bootstrap_Exception('Must invoke run function');
    }

    /**
     * _autoload
     * 加载类库 
     * 
     * @param  mixed $className 
     * @return void
     */
    protected static function _autoload($className) {

        $inflection = array('Smarty' => 'View/Smarty/Smarty.class.php',
                            );

        if (array_key_exists($className, $inflection)) {
            require_once ROOT_PATH . '/framework/library/' . $inflection[$className];
            return true;
        }

        $items = explode('_', $className);

        if ($items[0] != APP_NAME) {
            $file = ROOT_PATH . '/framework/library/' . implode('/', $items) . '.php';
        } else {
            $file = APP_PATH . '/include/' . implode('/', array_slice($items, 1)) . '.php';
        }

        if (!file_exists($file)) {
            throw new Bootstrap_Exception('Load class:' . $className . ' file:' . $file . ' failed.');
        }

        require_once $file;

        return true;
    }

    /**
     * _registExceptionHandler
     * 注册异常回调
     *
     * @return void
     */
    protected function _registExceptionHandler() {

        $exceptionHandler = $this->getOption('exceptionHandler');

        if ($exceptionHandler) {
            set_exception_handler($exceptionHandler);
        } else {
            set_exception_handler(array('Bootstrap_Abstract', 'exceptionHandler'));
        }
    }

    /**
     * exceptionHandler
     * 异常回调函数
     *
     * @param  object $exception Exception对象
     * @return void
     */
    public static function exceptionHandler($exception) {

        $msg = date('[Y-m-d H:i:s]') . "Uncaught exception: " . $exception->getMessage() . "\n";
        file_put_contents(APP_PATH . '/logs/exception/exception_' . date('Y_m-d') . '.log', $msg, FILE_APPEND);
    }

    /**
     * _registFunctions
     * 注册自定义函数
     *
     * @return boolean
     */
    protected function _registFunctions() {

        $path = ROOT_PATH . '/framework/function';
        if (!is_dir($path)) {
            return false;
        }

        $files = glob($path . '/*.php');
        foreach ($files as $file) {
            require_once($file);
        }

        return true;
    }
}
